from wtforms import StringField,Form
from wtforms.validators import length,ValidationError
from exts import db
from model.models import DishType


class addDishTypeForm(Form):
    dish_type_name = StringField(validators=[length(min=1,max=20,message='请输入合适长度的菜品名')])

    def validate_dish_type_name(self,field):
        dish_types = DishType.query.filter(DishType.dish_type_name == field.data).all()
        if dish_types:
            raise ValidationError("该菜品已存在")
        return True


class UpdateDishType(Form):
    old_dish_type_name = StringField(validators=[length(min=1,max=20,message='请输入合适长度的菜品名')])
    dish_type_name = StringField(validators=[length(min=1,max=20,message="请输入合适长度的菜品名")])

    def validate_dish_type_name(self,field):
        dish_types = DishType.query.filter(DishType.dish_type_name==field.data)
        if not dish_types:
            raise ValidationError("该菜品已存在")
        return True